import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.junit.Test;

import java.io.File;
import java.io.FileOutputStream;

/**
 * chance！Time
 *
 * @author liuqian
 * @date 2022-06-20 16:14
 * *
 */
public class WriteExcel {

    @Test
    public void creatExcel() throws Exception {
        //    创建工作簿，
        Workbook workbook = new XSSFWorkbook();
        //创建工作表
        Sheet sheet = workbook.createSheet("测试excel");
        //在工作表中创建行
        Row row = sheet.createRow(0);
        //使用行创建单元格
        Cell cell = row.createCell(0);
        //给单元格赋值
        //表头
        cell.setCellValue("姓名");
        row.createCell(1).setCellValue("年龄");
        row.createCell(2).setCellValue("所在地");

        row = sheet.createRow(1);
        row.createCell(0).setCellValue("小明");
        row.createCell(1).setCellValue(20);
        row.createCell(2).setCellValue("北京");
        row = sheet.createRow(2);
        row.createCell(0).setCellValue("小李");
        row.createCell(1).setCellValue(25);
        row.createCell(2).setCellValue("南京");

        workbook.write(new FileOutputStream(new File("d:\\createExcel.xlsx")));
        workbook.close();

    }
}
